import { setToken, removeToken } from '@/utils/auth'
import { setStore, getStore } from '@/utils/store'
import { isURL } from '@/utils/validate'
import { deepClone } from '@/utils/common'
import website from '@/const/website'
import { loginByUsername, getUserInfo, getMenu, logout, getMenuAll, RefeshToken } from '@/modules/api/user'

function addPath(ele, first) {
    const propsConfig = website.menu.props
    const propsDefault = {
        label: propsConfig.label || 'label',
        path: propsConfig.path || 'path',
        icon: propsConfig.icon || 'icon',
        children: propsConfig.children || 'children'
    }
    const isChild = ele[propsDefault.children] && ele[propsDefault.children].length !== 0
    if (!isChild && first) {
        ele[propsDefault.path] = ele[propsDefault.path] + '/index'
        return
    }
    ele[propsDefault.children].forEach(child => {
        if (!isURL(child[propsDefault.path])) {
            child[propsDefault.path] = `${ele[propsDefault.path]}/${child[propsDefault.path]?child[propsDefault.path]:'index'}`
        }
        addPath(child)
    })
}

const user = {
    state: {
        userInfo: {},
        permission: {},
        roles: [],
        menu: getStore({ name: 'menu' }) || [],
        menuAll: [],
        token: getStore({ name: 'token' }) || '',
    },
    actions: {
        //根据用户名登录
        LoginByUsername({ commit }, userInfo) {
            // const user = encryption({
            //     data: userInfo,
            //     type: 'Aes',
            //     key: 'avue',
            //     param: ['useranme', 'password']
            // })
            return new Promise((resolve) => {
                loginByUsername(userInfo.username, userInfo.password, userInfo.code).then(res => {
                    const data = res.data.data
                    commit('set_token', data.token.access.access_token)
                    commit('del_all_tag')
                    setToken(data.token.access.access_token)
                    resolve()
                })
            })
        },
        //根据手机号登录
        LoginByPhone({ commit }, userInfo) {
            return new Promise((resolve) => {
                loginByUsername(userInfo.phone, userInfo.code).then(res => {
                    const data = res.data.data
                    commit('set_token', data)
                    commit('del_all_tag')
                    setToken(data)
                    resolve()
                })
            })
        },
        GetUserInfo({ commit }) {
            return new Promise((resolve, reject) => {
                getUserInfo().then((res) => {
                    const data = res.data.data
                    commit('set_userInfo', data.user_info)
                    commit('set_roles', data.roles)
                    commit('set_permission', data.permission)
                    resolve(data)
                }).catch(err => {
                    reject(err)
                })
            })
        },
        //刷新token
        RefeshToken({ commit }) {
            return new Promise((resolve, reject) => {
                RefeshToken().then(res => {
                    const data = res.data.data
                    commit('set_token', data)
                    setToken(data)
                    resolve(data)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 登出
        LogOut({ commit }) {
            return new Promise((resolve, reject) => {
                logout().then(() => {
                    commit('set_token', '')
                    commit('set_menu', [])
                    //commit('set_roles', [])
                    commit('del_all_tag')
                    removeToken()
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },
        //注销session
        FedLogOut({ commit }) {
            return new Promise(resolve => {
                commit('set_token', '')
                commit('set_menu', [])
                //commit('set_roles', [])
                commit('del_all_tag')
                removeToken()
                resolve()
            })
        },
        //获取系统菜单
        GetMenu({ commit }, parentId) {
            return new Promise(resolve => {
                getMenu(parentId).then((res) => {
                    const data = res.data.data
                    let menu = deepClone(data)
                    menu.forEach(ele => {
                        addPath(ele, true)
                    })
                    commit('set_menu', menu)
                    resolve(menu)
                })
            })
        },
        //获取全部菜单
        GetMenuAll({ commit }) {
            return new Promise(resolve => {
                getMenuAll().then((res) => {
                    const data = res.data.data
                    commit('set_menu_all', data)
                    resolve(data)
                })
            })
        },

    },
    mutations: {
        set_token: (state, token) => {
            state.token = token
            setStore({ name: 'token', content: state.token, type: 'session' })
        },
        set_userInfo: (state, userInfo) => {
            state.userInfo = userInfo
        },
        set_menu: (state, menu) => {
            state.menu = menu
            setStore({ name: 'menu', content: state.menu, type: 'session' })
        },
        set_menu_all: (state, menuAll) => {
            state.menuAll = menuAll
        },
        set_roles: (state, roles) => {
            state.roles = roles
        },
        set_permission: (state, permission) => {
            state.permission = {}
            permission.forEach(ele => {
                state.permission[ele] = true
            })
        }
    }
}

export default user